package com.symbio.ishenapi.patient.controller;

import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.patient.model.TmPatientMedicalTemporary;
import com.symbio.ishenapi.patient.model.TmPatientmedical;
import io.jboot.web.controller.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TmMedicalRecordsController
 * @Description 病例模块
 * @Author GuoShunYou
 * @Date 2018/10/19  上午 10:09
 **/
@RequestMapping("/TmMedicalRecordsController")
public class TmMedicalRecordsController extends BaseController {



    /**
     *
     *
     * @Author GuoShunYou
     * @Description 根据userId查询是否已完善病例
     * @date 下午 6:09   2018/10/22 0022
     * @return void
     */
    public void isnoByUserId(){
        TmPatientmedical tpm=(TmPatientmedical)getReqBodyObject(TmPatientmedical.class);
        //通过userid查询对应患者id
        TmPatientmedical tmpamedi=TmPatientmedical.dao.getid(tpm.getUserId());
        //通过患者id查询对应是否有病例信息
         if (tmpamedi == null){
             renderJsonError(40012, "未完善病例");
             return;
         }
        int num=TmPatientMedicalTemporary.dao.getCountbyUserId(tmpamedi.getId());
        if(num>0){
            renderJsonError(40011, "已完善病例");
       }else{
            renderJsonError(40012, "未完善病例");
        }
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 病例信息保存
     * @date 上午 10:12   2018/10/19
     * @param
     * @return void
     */
    public void addMedicalRecords(){
        TmPatientMedicalTemporary tpm=(TmPatientMedicalTemporary)getReqBodyObject(TmPatientMedicalTemporary.class);
        //查询该患者是否已填过病例单
        int num=TmPatientMedicalTemporary.dao.getCountbyUserId(tpm.getPatientId());
        if(num==0){
            //  添加
            Date date=new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            tpm.setCreateDate(formatter.format(date));
            boolean b=tpm.save();
            if(b){
                renderJsonError(40001, "病例信息");
            }else{
                renderJsonError(40002, "病例信息");
            }
        }else {
            TmPatientMedicalTemporary tmpp=TmPatientMedicalTemporary.dao.getIdbyUserId(tpm.getPatientId());
            tpm.setId(tmpp.getId());
            //  更新
            boolean b=tpm.update();
            if(b){
                renderJsonError(40001, "病例信息");
            }else{
                renderJsonError(40002, "病例信息");
            }
        }
    }


    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通过userID查询所有病例表中的问题答案
     * @date 下午 3:41   2018/10/19
     * @param
     * @return void
     */
    public void selectMedical(){
        TmPatientmedical tpm=(TmPatientmedical)getReqBodyObject(TmPatientmedical.class);
        //通过userid查询对应患者信息
        TmPatientmedical tmpate=tpm.dao.getid(tpm.getUserId());
        //通过患者id查询对应数据
        List<TmPatientMedicalTemporary> lsit=TmPatientMedicalTemporary.dao.getListByUserId(tmpate.getId());
        if(lsit.size()<=0){
            renderJsonError(40003, "该患者的病例信息");
        }else{
            Map map=new HashMap<>();
            map.put("id",lsit.get(0).getId());
            map.put("patientId",lsit.get(0).getPatientId());
            map.put("userid",tpm.getUserId());
            lsit.get(0).setId(null);
            lsit.get(0).setPatientId(null);
            map.put("answer",lsit.get(0));


            renderJsonRet(map);
        }
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 添加/修改 病例所属人患者信息
     * @date 下午 4:30   2018/11/13
     * @return void
     */
    public void addTmpatienmedical(){
        TmPatientmedical tpm=(TmPatientmedical)getReqBodyObject(TmPatientmedical.class);
        List<TmPatientmedical> list=TmPatientmedical.dao.selectAll(tpm.getUserId());
        if(list.size()>0){
            tpm.setId(list.get(0).getId());
            if(tpm.update()){
                renderJsonRet(tpm.getId());
            }else{
                renderJsonError(40002, "患者基本信息");
            }
        }else{
            if(tpm.save()){
                renderJsonRet(tpm.getId());
            }else{
                renderJsonError(40002, "患者基本信息");
            }
        }


    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 通過userid查詢患者信息
     * @date 下午 4:33   2018/11/13
     * @return void
     */
    public void selectpatientmedical(){
        TmPatientmedical tpm=(TmPatientmedical)getReqBodyObject(TmPatientmedical.class);
        List<TmPatientmedical> list=TmPatientmedical.dao.selectAll(tpm.getUserId());
        /*JbootRedis redis = Jboot.me().getRedis();
        String str="ssdsdsddsdsdsdssd";
        redis.set(str,list);
        System.out.println(redis.get(str).toString());*/
        if(list.size()>0){
            renderJsonRet(list.get(0));
            return;
        }else{
            renderJsonError(40003,"该userid对应");
            return;
        }

    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 修改患者基本信息表
     * @date 下午 4:37   2018/11/13
     * @return void
     */
    public void updatepatientmedical(){
        TmPatientmedical tpm=(TmPatientmedical)getReqBodyObject(TmPatientmedical.class);
        if(tpm.update()){
            renderJsonError(40004,"患者基本信息");
        }else{
            renderJsonError(40005,"患者基本信息");
        }
    }

    /**
     *
     *
     * @Author GuoShunYou
     * @Description 刪除患者基本信息
     * @date 下午 4:40   2018/11/13
     * @return
     */
    public void delpatientmedical(){
        TmPatientmedical tpm=(TmPatientmedical)getReqBodyObject(TmPatientmedical.class);
        tpm.setStatus("0");
        if(tpm.update()){
            renderJsonError(50001,"刪除");
        }else{
            renderJsonError(50002,"刪除");
        }


    }


    /**
     * wy:根据用户ID查询该用户所关联的病历信息
     */
    public void findByUserIdGetMedical(){       //根据条件查询显示患者列表
        String userId = getPara("userId");
        TmPatientmedical tpm = new TmPatientmedical();
        Map map = tpm.findByUserIdGetMedical(getQueryModel(),userId);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * wy:查询病历详情
     */
    public void findByIdGetM(){
        String id = getPara("userId");
        if (id != null && !"".equals(id)) {
            TmPatientMedicalTemporary tpm = new TmPatientMedicalTemporary();
            TmPatientMedicalTemporary temporary = tpm.getIdbyUserId(id);
            renderJsonRet(temporary);
            return;
        }else {
            renderJsonError(40003,"查询");
        }
    }



}
